package com.itheima.module.web.controller.upload;


import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@ResponseBody
public class UploadController {
    @RequestMapping("/upload")
    public String upload(
            @RequestParam("username") String username,
            @RequestParam("email")String email,
            MultipartFile avatar


    ) throws IOException, FileUploadException {

        /*//获取请求头中 请求体数据的长度
        int len= Integer.parseInt(request.getHeader("content-length"));

        //读取请求体中数据
        byte[] buff=new byte[len];

        request.getInputStream().read(buff);

        System.out.println(new String(buff,"utf-8"));*/


        //直接使用 工具包
        /*DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
        FileUpload fileUpload = new FileUpload(diskFileItemFactory);

        List<FileItem> fileItems = fileUpload.parseRequest(request);

        System.out.println(fileItems);*/

        System.out.println(avatar);

        //获取文件名字
        String filename = avatar.getOriginalFilename();
        //文件类型
        String contentType = avatar.getContentType();
        //文件长度
        avatar.getSize();
        //把文件换个位置
        avatar.transferTo(new File("d://uploads/"+filename));








        return "success";
    }
}
